/*
 * @Descripttion: 
 * @Author: yuxi
 * @Date: 2022-09-15 15:47:32
 * @LastEditors: yuxi
 * @LastEditTime: 2022-10-11 22:00:02
 */
const types = {
  control: {
    unload: 'removeControl'
  },
  layer: {
    unload: 'removeTileLayer'
  },
  overlay: {
    unload: 'removeOverlay'
  },
  contextMenu: {
    unload: 'removeContextMenu'
  }
}

const getParent = $component => ($component.abstract || $component.$el === $component.$children[0].$el) ? getParent($component.$parent) : $component

function destroyInstance () {
  const { unload, renderByParent, $parent } = this
  if (renderByParent) {
    $parent.reload()
  }
  unload()
}

class Mixin {
  constructor(prop) {
    // console.log('prop', prop)
    this.props = {
      layerName: {
        type: String,
        default () {
          return ''
        }
      },
      layerId: {
        type: String,
        default () {
          return ''
        }
      },
      addLayerControl: {
        type: Boolean,
        default () {
          return true
        }
      },
      editAble: {
        type: Boolean,
        default () {
          return false
        }
      },
      properties: {
        type: Object,
        default () {
          return {}
        }
      },
    }
    this.methods = {
      ready () {
        const $parent = getParent(this.$parent)
        const map = this.map = $parent.map
        const layer = this.layer = $parent.layer

        this.load()
        if(layer) {
          this.$emit('ready', {
            map,
            layer
          })
        } else {
          this.$emit('ready', {
            map
          })
        }
       
      },
      transmitEvent (e) {
        this.$emit(e.type.replace(/^on/, ''), e)
      },
      reload () {
        // this && this.map && this.$nextTick(() => {
        //   this.unload()
        //   this.$nextTick(this.load)
        // })
      },
      unload () {
        // const { map, originInstance } = this
        // try {
        //   switch (prop.type) {
        //     case 'search':
        //       return originInstance.clearResults()
        //     case 'autoComplete':
        //     case 'lushu':
        //       return originInstance.dispose()
        //     case 'markerClusterer':
        //       return originInstance.clearMarkers()
        //     default:
        //       map[types[prop.type].unload](originInstance)
        //   }
        // } catch (e) { console.error(e) }
      }
    }
    this.computed = {
      renderByParent () {
        return this.$parent.preventChildrenRender
      }
    }
    this.mounted = function () {
      console.log(`BaseMixin mounted ${prop.type}`)
      const $parent = getParent(this.$parent)
      const map = $parent.map
      const layer = $parent.layer

      const { ready } = this

      switch (prop.type) {
        case 'feature':
          console.log('match feature')
          layer ? ready() : $parent.$on('ready', ready)
          break;
        default:
          map ? ready() : $parent.$on('ready', ready)
          break;
      }

      

    }
    this.destroyed = destroyInstance
    this.beforeDestroy = destroyInstance
  }
}

export default type => new Mixin({ type })